/*
Using publicly available Medicare Part D and NPI data

// Step 1: import the two datasets
// Step 2: analyze market shares
*/

clear
import delimited using "PartD_Prescriber_PUF_NPI_Drug_14.tsv", varn(1)

save Medicare_PartD_2014_data, replace



clear

import delimited using "National_Downloadable_File.csv", varn(nonames)

rename v1 npi
rename v4 last_name
rename v5 first_name
rename v8 gender
rename v9 credential
rename v10 med_school
rename v11 grad_yr // med school graduation year
rename v12 specialty


drop v*

duplicates drop

// different specialty in different places
duplicates tag npi, gen(t)
tab t
drop t

egen t = tag(npi)
keep if t == 1
drop t


display _N

save physician_info_2014, replace




clear
use Medicare_PartD_2014_data

merge m:1 npi using physician_info_2014, keepus(gender credential med_school grad_yr specialty)
keep if _merge == 3
drop _merge

// LOOK AT CHOLESTEROL DRUGS

keep if generic_name == "ATORVASTATIN CALCIUM" | ///
generic_name == "SIMVASTATIN" | ///
generic_name == "ROSUVASTATIN CALCIUM" | ///
generic_name == "LOVASTATIN" | ///
generic_name == "FLUVASTATIN SODIUM" | ///
generic_name == "PITAVASTATIN CALCIUM" | ///
generic_name == "EZETIMIBE" | ///
generic_name == "EZETIMIBE/SIMVASTATIN" | ///
generic_name == "NIACIN/SIMVASTATIN"

gen brand = drug_name == "LIPITOR" | ///
drug_name == "ZOCOR" | ///
drug_name == "CRESTOR" | ///
drug_name == "LESCOL" | drug_name == "LESCOL XR" | ///
drug_name == "LIVALO" | ///
drug_name == "ZETIA" | ///
drug_name == "VYTORIN" | ///
drug_name == "SIMCOR"


// COLLAPSE BY YEAR

collapse (sum) bene_count total_claim_count total_day_supply total_drug_cost, by(generic_name brand grad_yr)

gen brand_total_day_supply = brand * total_day_supply


// compute market shares
foreach v in bene_count brand_total_day_supply total_claim_count total_day_supply total_drug_cost {

	egen tot_`v' = sum(`v'), by(grad_yr)
	gen ms_`v' = `v' / tot_`v'
}

destring grad_yr, replace force
drop if grad_yr == . | grad_yr < 1970


  
 
 
 // graph something (e.g. Lipitor, Zocor, Crestor, Mevacor share in 2014 by year)
twoway (line ms_bene_count grad_yr if generic_name == "LOVASTATIN") /// 
 (line ms_bene_count grad_yr if generic_name == "ATORVASTATIN CALCIUM" & brand == 0) ///
 (line ms_bene_count grad_yr if generic_name == "SIMVASTATIN" & brand == 0) ///
 (line ms_bene_count grad_yr if generic_name == "ROSUVASTATIN CALCIUM"), ///
xtitle("Medical School Graduation Year") ytitle("Market Share") ///
legend(label(1 "Mevacor") label(2 "Lipitor (G)") label(3 "Zocor (G)") label(4 "Crestor"))
 
graph save market_share_2014_by_grad_yr, replace
graph export market_share_2014_by_grad_yr.pdf, replace
 

 save market_shares_by_grad_yr_2014, replace
 
 
 

 
 
 /*
 Number of different options
 */
 
clear
use Medicare_PartD_2014_data

merge m:1 npi using physician_info_2014, keepus(gender credential med_school grad_yr specialty)
keep if _merge == 3
drop _merge

 
 
 // LOOK AT CHOLESTEROL DRUGS

keep if generic_name == "ATORVASTATIN CALCIUM" | ///
generic_name == "SIMVASTATIN" | ///
generic_name == "ROSUVASTATIN CALCIUM" | ///
generic_name == "LOVASTATIN" | ///
generic_name == "FLUVASTATIN SODIUM" | ///
generic_name == "PITAVASTATIN CALCIUM" | ///
generic_name == "EZETIMIBE" | ///
generic_name == "EZETIMIBE/SIMVASTATIN" | ///
generic_name == "NIACIN/SIMVASTATIN"

gen brand = drug_name == "LIPITOR" | ///
drug_name == "ZOCOR" | ///
drug_name == "CRESTOR" | ///
drug_name == "LESCOL" | drug_name == "LESCOL XR" | ///
drug_name == "LIVALO" | ///
drug_name == "ZETIA" | ///
drug_name == "VYTORIN" | ///
drug_name == "SIMCOR"
 

 gen options = 1
 gen dummy = 1
 collapse (count) dummy, by(npi generic_name)
 collapse (sum) options, by(npi)
 
 display _N
 hist options
 
 gen doctors = 1
 collapse (count) doctors, by(options)
 
 sort options
 save medicare_partd_variety, replace
 
 
 
 

